﻿using AutoMapper;
using IntelligentAgriculture.Contracts.Dto.Input;
using IntelligentAgriculture.Contracts.Dto.Output;
using IntelligentAgriculture.Contracts.Interface;
using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Services.StartServices;
using IntelligentAgriculture.WebAPI.Application.Command.StartCommand;
using IntelligentAgriculture.WebAPI.Application.Command.UserCommand;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace IntelligentAgriculture.WebAPI.Controllers
{
    /// <summary>
    /// 期初管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class StartController : ControllerBase
    {

        /// <summary>
        /// 中介者模式
        /// </summary>
        private readonly IMediator _mediator;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<StartController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者模式</param>
        /// <param name="logger">日志</param>
        public StartController(IMediator mediator, ILogger<StartController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }


        /// <summary>
        /// 期初查询
        /// </summary>
        /// <param name="input">输入参数</param>
        /// <returns>返回数据</returns>
        [HttpGet]
        public async Task<Results<PageOutput<StartOutput>>> GetStartList([FromQuery]StartInput? input)
        {
            try
            {
                _logger.LogInformation("这是显示查询期初表");
                var list = new StartSelectCommand(input);
                return await _mediator.Send(list);
            }
            catch (Exception ex)
            {
                _logger.LogError("这是显示查询期初表发生异常" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 添加期初表数据
        /// </summary>
        /// <param name="command">实体</param>
        /// <returns>返回受影响的行数</returns>

        [HttpPost]
        public async Task<Results<int>> AddStart(List<Start> input)
        {
            try
            {
                _logger.LogInformation("这是期初添加");
                var command = new StartCreateCommand(input);
                return await _mediator.Send(command);
               
            }
            catch (Exception ex)
            {
                _logger.LogError("这是期初添加发生异常" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 逻辑删除期初数据
        /// </summary>
        /// <param name="command">主键id</param>
        /// <returns>受影响的行数</returns>
        /// 

        [HttpDelete]
        public async Task<Results<int>> DeleteStart(StartDeleteCommand command)
        {
            try
            {
                _logger.LogInformation("这是期初逻辑删除");


                return await _mediator.Send(command);

            }
            catch (Exception ex)
            {
                _logger.LogError("这是期初逻辑删除发生异常" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 逻辑批量删除期初数据
        /// </summary>
        /// <param name="command">主键id</param>
        /// <returns>受影响的行数</returns>
        /// 

        [HttpPut]
        public async Task<Results<int>> DeleteRangeStart(StartDeleteRangeCommand command)
        {
            try
            {
                _logger.LogInformation("这是期初逻辑批量删除");


                return await _mediator.Send(command);

            }
            catch (Exception ex)
            {
                _logger.LogError("这是期初逻辑批量删除发生异常" + ex.Message);
                throw;
            }
        }




        /// <summary>
        /// 反填期初表
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns>返回数据</returns>
        [HttpGet]
        public async Task<Results<StartOutput>> GetStartById(int id)
        {
            try
            {
                _logger.LogInformation("这是反填期初表");
                var query = new StartByIdCommand(id);
                var start = await _mediator.Send(query);
                return start;
            }
            catch (Exception ex)
            {
                _logger.LogError("这是反填期初表发生异常" + ex.Message);
                throw;
            }
            
        }


        /// <summary>
        /// 修改期初表
        /// </summary>
        /// <param name="command">期初实体</param>
        /// <returns>受影响的行数</returns>
        [HttpPut]
        public async Task<Results<int>> UpdateStart(StartUpdateCommand command)
        {
            try
            {
                _logger.LogInformation("这是修改期初表");
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError("这是修改期初表发生异常" + ex.Message);
                throw;
            }
           
        }


        /// <summary>
        /// 期初表数据导出
        /// </summary>
        /// <param name="input">参数</param>
        /// <returns>返回数据</returns>
        [HttpGet]
        public async Task<IActionResult> ExportStart([FromQuery]StartInput input)
        {
            try
            {

                try
                {
                    _logger.LogInformation("这是期初表数据导出");
                    var list = new StartExportCommand(input);

                    return await _mediator.Send(list);
                }
                catch (Exception ex)
                {
                    _logger.LogError("这是期初表数据导出发生异常" + ex.Message);
                    throw;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 期初导入
        /// </summary>
        /// <param name="file">文件</param>
        /// <returns>返回数据</returns>
        [HttpPost("import")]
        public async Task<Results<IActionResult>> ImportStartData(IFormFile file)
        {
            try
            {
                var command = new StartImportCommand(file);
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError($"期初导入数据失败: {ex.Message}");
                throw;
            }
        }













    }
}
